import {UserActions} from './user';

const initialState = {};

export const SupplierExamineListActions = {
  SUPPLIER_EXAMINE_LIST_UPDATE: 'supplier_examine_list_update',
  SUPPLIER_EXAMINE_LIST_BATCH_UPDATE: 'supplier_examine_list_batch_update',
  SUPPLIER_EXAMINE_LIST_RESET: 'supplier_examine_list_reset',
};

export function updateSupplierExamineList(payload) {
  return {
    type: SupplierExamineListActions.SUPPLIER_EXAMINE_LIST_UPDATE,
    payload,
  };
}

export function batchUpdateSupplierExamineList(payload) {
  return {
    type: SupplierExamineListActions.SUPPLIER_EXAMINE_LIST_BATCH_UPDATE,
    payload,
  };
}

export function resetSupplierExamineList() {
  return {type: SupplierExamineListActions.SUPPLIER_EXAMINE_LIST_RESET};
}

export default function supplierExamineList(state = initialState, action) {
  switch (action.type) {
    case SupplierExamineListActions.SUPPLIER_EXAMINE_LIST_UPDATE:
      return {
        ...state,
        [action.payload.id]: {...state[action.payload.id], ...action.payload},
      };
    case SupplierExamineListActions.SUPPLIER_EXAMINE_LIST_BATCH_UPDATE:
      action.payload.forEach(obj => {
        state[obj.id] = obj;
      });
      return {...state};
    case SupplierExamineListActions.SUPPLIER_EXAMINE_LIST_RESET:
      return {};
    case UserActions.USER_SIGN_OUT:
      return {};
    default:
      return state;
  }
}
